import Actions from './actions';
import DB from '../../app/db';
import {fromJS} from 'immutable';
import Reflux from 'reflux';
import nattyStorage from 'natty-storage';
import {menuList, slideImgList} from './../../contas/contas.js';

const {APP_URL} = require(`./../../config/${ENV}.json`);


const storageUser = nattyStorage({
  type: 'localStorage',
  key: 'sskj-dingtalk-user',
});


module.exports = Reflux.createStore({
  listenables: [Actions],
  data: {
    user: fromJS(storageUser),
    isRun: false,
    menuList: [],
    slideImgList: slideImgList,
    rwList: [{
      id: 'db',
      text: '待办事项',
      route:APP_URL + '#dbrw',
      //route: '/dbrw',
      value: '0',

    }, {
      id: 'notice',
      text: '提 醒',
      route: '',
      value: '0',

    }, {
      id: 'zsgc',
      text: '在手工程',
      route: APP_URL + '#englist',
      //route: '/englist',
      value: '0',

    }, {
      id: 'nbtz',
      text: '内部通知',
      route: APP_URL + '#nbtzlist',
      value: '0',

    }],


  },


  onEngCount: function () {
    let t = this;

    if (t.data.user.get('name')) {
      DB.eng.count({
        jdfzr: t.data.user.get('name'),
      }).then(
        function (content) {
          t.data.rwList.map((v, k) => {
            if (v.id == 'zsgc') {
              v.value = content;
            }
          });
          t.updateComponent();

        }, function (error) {
          console.log(error);
        })
    }
  },


  onMessageCount: function () {
    let t = this;

    if (t.data.user.get('userid')) {
      DB.message.count({
        userId: t.data.user.get('userid'),
      }).then(
        function (content) {
          t.data.rwList.map((v, k) => {
            if (v.id == 'nbtz') {
              v.value = content;
            }
          });
          t.updateComponent();

        }, function (error) {

        })
    }
  },

  onInitMenu: function () {
    let t = this;

    t.data.menuList = menuList.filter((item) => {

      if (t.data.user.get('isAdmin')) {
        return item
      } else {
        if (item.id != 'tzgg') {
          return item;
        }
      }
    });

    t.updateComponent();

  },


  onFetchDbsx: function () {
    let t = this;

    if(__LOCAL__ === true)
    {
      t.data.user.set('userid', '4028801c2d91caf8012d9361b51b0060');
      t.data.user.set('name', '张坤');
    }

    if (t.data.user.get('userid')) {
      DB.jdrwmx.db.get.count({
        userid: t.data.user.get('userid'),
        isRun: t.data.isRun,
      }).then(
        function (content) {
          t.data.rwList.map((v, k) => {
            if (v.id == 'db') {
              v.value = content;
            }
          });
          t.updateComponent();

        }, function (error) {

        })
    }
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
